/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.extremecomponents.table.view;

import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.core.EccnTableConstants;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.DefaultStatusBar;
import org.extremecomponents.table.view.View;
import org.extremecomponents.table.view.html.CalcBuilder;
import org.extremecomponents.table.view.html.FormBuilder;
import org.extremecomponents.table.view.html.RowBuilder;
import org.extremecomponents.table.view.html.TableBuilder;
import org.extremecomponents.util.HtmlBuilder;

/**
 * @author guanhw
 */
public class EccnHtmlView implements View {
	private HtmlBuilder		html;
	private TableBuilder	tableBuilder;
	private RowBuilder		rowBuilder;
	private FormBuilder		formBuilder;
	private CalcBuilder		calcBuilder;
	boolean					bufferView;
	
	protected HtmlBuilder getHtmlBuilder ( ) {
		return html;
	}
	
	protected CalcBuilder getCalcBuilder ( ) {
		return calcBuilder;
	}
	
	protected FormBuilder getFormBuilder ( ) {
		return formBuilder;
	}
	
	protected RowBuilder getRowBuilder ( ) {
		return rowBuilder;
	}
	
	protected TableBuilder getTableBuilder ( ) {
		return tableBuilder;
	}
	
	public void beforeBody ( TableModel model ) {
		bufferView = model.getTableHandler ( ).getTable ( ).isBufferView ( );
		if ( bufferView ) {
			html = new HtmlBuilder ( );
		} else {
			html = new HtmlBuilder ( model.getContext ( ).getWriter ( ) );
		}
		tableBuilder = new TableBuilder ( html , model );
		rowBuilder = new RowBuilder ( html , model );
		formBuilder = new FormBuilder ( html , model );
		calcBuilder = new CalcBuilder ( html , model );
		formBuilder.formStart ( );
		tableBuilder.themeStart ( );
		tableBuilder.tableStart ( );
		tableBuilder.theadStart ( );
		tableBuilder.title ( );
		// tableBuilder.titleRowSpanColumns();
		String paginationLocation = ( String ) model.getTableHandler ( )
				.getTable ( ).getAttribute ( "paginationLocation" );
		if ( ! EccnTableConstants.PAGINATION_LOCATION_DOWN
				.equalsIgnoreCase ( paginationLocation ) ) {
			toolbar ( model );
			// statusBar( model );
			tableBuilder.filterRow ( );
		}
		tableBuilder.headerRow ( );
		tableBuilder.theadEnd ( );
		tableBuilder.tbodyStart ( );
	}
	
	public void body ( TableModel model , Column column ) {
		if ( column.isFirstColumn ( ) ) {
			rowBuilder.rowStart ( );
		}
		html.append ( column.getCellDisplay ( ) );
		if ( column.isLastColumn ( ) ) {
			rowBuilder.rowEnd ( );
		}
	}
	
	public Object afterBody ( TableModel model ) {
		calcBuilder.defaultCalcLayout ( );
		String paginationLocation = ( String ) model.getTableHandler ( )
				.getTable ( ).getAttribute ( "paginationLocation" );
		tableBuilder.tbodyEnd ( );
		if ( EccnTableConstants.PAGINATION_LOCATION_DOWN
				.equalsIgnoreCase ( paginationLocation ) ) {
			tableBuilder.filterRow ( );
			// statusBar( model );
			toolbar ( model );
		}
		tableBuilder.tableEnd ( );
		tableBuilder.themeEnd ( );
		formBuilder.formEnd ( );
		if ( this.bufferView ) {
			return html.toString ( );
		}
		return "";
	}
	
	protected void toolbar ( TableModel model ) {
		new EccnToolbar ( getHtmlBuilder ( ) , model ).layout ( );
	}
	
	protected void titleBar ( TableModel model ) {
		new DefaultStatusBar ( getHtmlBuilder ( ) , model ).layout ( );
	}
}
